﻿-- Type a insérer
DECLARE @TypeId UNIQUEIDENTIFIER
SELECT @TypeId = '45C90CDA-E48D-4F9E-A5A4-1C398B9DD5F5'

-- In sertion dans la table Type
SELECT 
'IF NOT EXISTS( SELECT NULL  FROM medianamik_Type where TypeId = ''' + CAST(TypeId as NVARCHAR(38)) + ''')
BEGIN
INSERT INTO [dbo].[medianamik_Type]
           ([TypeId]
           ,[ParentTypeId]
           ,[Name]
           ,[CanHaveChilds]
           ,[IsGenerated]
           ,[IsManyToMany]
           ,[Path]
           ,[Depth]
           ,[Roles]
           ,[Flags]
           ,[Description]
           ,[ExtendedProperties]
           ,[IsSystem]
           ,[IsAbstract])
     VALUES
           ('''+CAST(TypeId as nvarchar(38))+'''
           ,'+ISNULL(''''+CAST(ParentTypeId as NVARCHAR(38))+'''', 'NULL')+'
           ,'''+Name+'''
           ,'+STr(CanHaveChilds)+'
           ,'+STr(IsGenerated)+'
           ,'+STR(ISManyToMany)+'
           ,'''+Path+'''
           ,'+STR(Depth)+'
			,NULL
			,'+STR(Flags)+'
           ,'+ISNULL('''' + REPLACE(Description, '''', '''''' )+'''','NULL' )+'
			,'+ISNULL('''' + CAST(ExtendedProperties as NVARCHAR(MAX))+'''','NULL' )+'
          ,'+STR(IsSystem)+'
			,'+STR(IsAbstract)+'
           )
END'

 from medianamik_Type where typeId =@TypeID

--Insertion dans la table Property
union
SELECT 'IF EXISTS( SELECT NULL FROM medianamik_Property where PropertyId = '''+cast(PropertyId as nvarchar(38))+''' )
BEGIN
UPDATE  [dbo].[medianamik_Property]
			SET [TypeId] = '''+CAST(TypeId as NVARCHAR(38))+'''
           ,[Name] = '''+Name+'''
           ,[HaveTooltip] = '+STR(HaveTooltip)+'
           ,[IsLanguageNeutral] = '+STR(IsLanguageNeutral)+'
           ,[IsRequired] ='+STR(IsRequired)+'
           ,[IsUnique] ='+STR(IsUnique)+'
           ,[ValidationRegex] ='+ISNULL(''''+ValidationRegex+'''', 'NULL')+'
           ,[GroupName] ='+ISNULL(''''+GroupName+'''', 'NULL')+'
           ,[Flags] ='+STR(Flags)+'
           ,[MedianamikControlId] ='+ISNULL(''''+CAST([MedianamikControlId] as NVARCHAR(38))+'''', 'NULL') + '
           ,[MedianamikControlWidth] ='+ISNULL(''''+[MedianamikControlWidth]+'''', 'null')+'
           ,[MedianamikControlHeight] ='+isnull(cast([MedianamikControlHeight] as varchar),'null')+'
           ,[Position] ='+STR(Position)+'
           ,[PrimaryPropertyId] ='+ISNULL(''''+CAST([PrimaryPropertyId] as NVARCHAR(38))+'''', 'NULL') +'
           ,[PrimaryTypeId] ='+ISNULL(''''+CAST([PrimaryTypeId] as NVARCHAR(38))+'''', 'NULL') + '
           ,[PrimaryMedianamikControlId] ='+ISNULL(''''+CAST([PrimaryMedianamikControlId] as NVARCHAR(38))+'''', 'NULL') + '
           ,[PrimaryMedianamikControlWidth] ='+ISNULL(''''+[PrimaryMedianamikControlWidth]+'''', 'null')+'
           ,[PrimaryMedianamikControlHeight] ='+isnull(cast([PrimaryMedianamikControlHeight] as varchar),'null')+'
           ,[Roles] ='+ISNULL(''''+Roles+'''', 'null')+'
           ,[MaxLength] ='+ISNULL(STR(MaxLength), 'NULL')+'
           ,[MinLength] ='+ISNULL(STR(MinLength), 'NULL')+'
           ,[DataTypeId] ='''+CAST(DataTypeId as NVARCHAR(38))+'''
           ,[PrimaryDataTypeId] ='+ISNULL(''''+CAST([PrimaryDataTypeId] as NVARCHAR(38))+'''', 'NULL') + '
           ,[Description] ='+ISNULL(''''+REPLACE(Description, '''', '''''' )+'''', 'NULL')+'
           ,[TreeRootNodeId] ='+ISNULL(''''+CAST([TreeRootNodeId] as NVARCHAR(38))+'''', 'NULL') + '
           , [PrimaryTreeRootNodeId] ='+ISNULL(''''+CAST([PrimaryTreeRootNodeId] as NVARCHAR(38))+'''', 'NULL') + '
			WHERE [PropertyId] = '''+CAST(PropertyId as NVARCHAR(38))+'''
           
END'

from Medianamik_Property
where typeId=@TypeId
